﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace GeodeticCoordinates
{
    public partial class SetParameters : Form
    {
        /// <summary>
        /// 判断投影方式 3 or 6
        /// </summary>
        private int _n;
        public int n { get; set; }

        /// <summary>
        /// 带号
        /// </summary>
        private int _n0;
        public int n0 { get; set; }

        /// <summary>
        /// 用于换带功能的，如果是3，则选择6，相反也是一个道理
        /// </summary>
        private int _pos;
        public int pos { get; set; }

        /// <summary>
        /// 输出椭球参数，和主窗口的功能一样
        /// </summary>
        public string[] ellispe { get; set; }
        private string[] _ellispe;

        /// <summary>
        /// 用于判断主窗口的combox控件是否可用，将两者联系起来
        /// </summary>
        public bool comvis { get { return _comvis; } set { _comvis = value; } }
        private bool _comvis = true;

        public SetParameters()
        {
            InitializeComponent();
        }

        private void comboBox_selectEllipsoid_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 获取椭球对应参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox_selectparameters_SelectedIndexChanged(object sender, EventArgs e)
        {
            EllipsoidParameters e1 = new EllipsoidParameters();
            _ellispe = e1.SetEllipse(comboBox_selectparameters.SelectedIndex); //获得当前椭球索引

            textBox_EllipsoidName.Text = _ellispe[0];
            textBox_Longhalfaxis.Text = _ellispe[1];
            textBox_Shorthalfaxis.Text = _ellispe[2];
            textBox_power_e1.Text = _ellispe[3]; //输出第一偏心率
            textBox_power_e2.Text = _ellispe[4];
            textBox_Flattening.Text = _ellispe[5]; //计算扁率f
            textBox_RadiusOfExtremecurvature.Text = _ellispe[6]; //计算极曲率半径
            _comvis = false;

            textBox_EllipsoidName.Enabled = false;
            textBox_Longhalfaxis.Enabled = false;
            textBox_Shorthalfaxis.Enabled = false;
            textBox_power_e1.Enabled = false;
            textBox_power_e2.Enabled = false;
            textBox_Flattening.Enabled = false;
            textBox_RadiusOfExtremecurvature.Enabled = false;
        }

        private void comboBox_touyingStyle_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 高斯投影正算功能
        /// </summary>
        private void GaosiTouyingSelect01()
        {
            int iPos = comboBox_touyingStyle.SelectedIndex;

            this.comboBox_touyingStyle.Enabled= true;
            this.textBox_lontitude.Enabled = true;
            this.textBox_centerlontitude.Enabled = false;
            this.textBox_daihao.Enabled = false;
            this.comboBox_ortient.Visible = false;
            this.label10.Visible = false;

            if (textBox_lontitude.Text == "")
            {
                MessageBox.Show("请输入经度值");
            }
            else
            {
                Calculate calculate = new Calculate();
                double[] result = calculate.GetL0(
                    Convert.ToDouble(textBox_lontitude.Text),
                    Convert.ToInt16(comboBox_touyingStyle.Items[iPos].ToString()));

                textBox_daihao.Text = result[0].ToString();
                textBox_centerlontitude.Text = result[1].ToString();
            }
        }

        /// <summary>
        /// 高斯投影反算功能
        /// </summary>
        private void GaosiTouyingSelect02()
        {
            int iPos = comboBox_touyingStyle.SelectedIndex;
            this.comboBox_touyingStyle.Enabled=true;
            this.textBox_lontitude.Enabled = false;
            this.textBox_daihao.Enabled = true;
            this.textBox_centerlontitude.Enabled = false;

            this.comboBox_ortient.Visible = false;
            this.label10.Visible = false;

            if (textBox_daihao.Text == "")
            {
                MessageBox.Show("请输入投影带号以及投影方式");
            }
            else if (iPos == 0 && Convert.ToInt16(textBox_daihao.Text) > 120 || Convert.ToInt16(textBox_daihao.Text) < 0)
            {
                MessageBox.Show("出错了！超过投影范围！请重新输入正确的投影带号以及投影方式");
            }
            else if (iPos == 1 && Convert.ToInt16(textBox_daihao.Text) > 60 || Convert.ToInt16(textBox_daihao.Text) < 0)
            {
                MessageBox.Show("出错了！超出投影范围！请重新输入正确的投影带号以及投影方式");
            }
            else
            {
                Calculate calculate = new Calculate();
                double result = calculate.GetL0(
                    Convert.ToInt16(textBox_daihao.Text),
                    Convert.ToInt16(comboBox_touyingStyle.Items[iPos].ToString()));

                textBox_centerlontitude.Text = result.ToString();
            }

        }

        /// <summary>
        /// 换带计算功能
        /// </summary>
        private void GaosiTouyingSelect03()
        {
            comboBox_touyingStyleChange();

            this.textBox_lontitude.Enabled = false;
            this.textBox_centerlontitude.Enabled = false;
            this.textBox_daihao.Enabled = true;
            this.comboBox_touyingStyle.Enabled = false;
        }

        /// <summary>
        /// 选择换带功能后手动添加新的combox控件
        /// </summary>
        private void comboBox_touyingStyleChange()
        {

            this.comboBox_ortient.Visible = true;
            this.label10.Visible = true;
           // this.comboBox_ortient.Items.Clear();
            DataTable other = new DataTable();
            other.Columns.Add("Text");//显示值
            other.Columns.Add("Value");//实际值
            DataRow dr1 = other.NewRow();
            DataRow dr2 = other.NewRow();
            dr1["Text"] = "从3°带转成6°带";
            dr1["Value"] = "从3°带转成6°带";
            dr2["Text"] = "从6°带转成3°带";
            dr2["Value"] = "从6°带转成3°带";
            other.Rows.Add(dr1);
            other.Rows.Add(dr2);
            comboBox_ortient.DataSource = other;
            comboBox_ortient.DisplayMember = "Text";
            comboBox_ortient.ValueMember = "Value";
            comboBox_ortient.SelectedIndex = -1;
        }

        private void comboBox_all_SelectedIndexChanged(object sender, EventArgs e)
        {
            int iPos = comboBox_all.SelectedIndex;
            switch (iPos)
            {
                case 0:
                    GaosiTouyingSelect01();
                    break;
                case 1:
                    GaosiTouyingSelect02();
                    break;
                case 2:
                    GaosiTouyingSelect03();
                    break;
            }
        }

        /// <summary>
        /// 将带号和投影方式组成int[]
        /// </summary>
        /// <returns>带号以及投影方式</returns>
        public int[] XX()
        {

            int[] XX = new int[2];
            _n = Convert.ToInt16(comboBox_touyingStyle.Items[comboBox_touyingStyle.SelectedIndex]); // 6 or 3
            _n0 = Convert.ToInt16(textBox_daihao.Text); //带号
            XX[0] = _n;
            XX[1] = _n0;
            return XX;
        }

        /// <summary>
        /// 点击确认按钮完成参数设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox_lontitude.Enabled == true)
            {
                GaosiTouyingSelect01();
                comvis = _comvis;
                ellispe = _ellispe;
            }
            else if (this.textBox_lontitude.Enabled == false && comboBox_ortient.Visible == false)
            {
                GaosiTouyingSelect02();
                comvis = _comvis;
                ellispe = _ellispe;
            }
            else if (comboBox_ortient.Visible == true)
            {
                pos = _pos;
                n0 = _n0;
                comvis = _comvis;
                ellispe = _ellispe;
            }
        }

        private void textBox_daihao_TextChanged(object sender, EventArgs e)
        {
            _n0 = Convert.ToInt16(textBox_daihao.Text);
        }

        private void comboBox_ortient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox_ortient.SelectedIndex == 0)
                _pos = 6;
            else
                _pos = 3;
        }
    }
}
